﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Data.Common;
using EtlComm;
using System.Diagnostics;

namespace ETLHandler
{
    class CIBExtract : IExtracter
    {

        #region IExtracter 成员

        public string ip { set; get; }

        public int TaskID { set; get; }

        public string StationID { set; get; }

        public string PrimaryKey { set; get; }

        private int _MaxId;

        public DataTable GetResult(string strSQL)
        {
            Debug.Assert(!string.IsNullOrEmpty(strSQL));
            DataTable dt = null;
            DbProviderFactory _DbFactory = DbProviderFactories.GetFactory("Borland.Data.AdoDbxClient");
            using (IDbConnection c = _DbFactory.CreateConnection())
            {
                c.ConnectionString = ip;
                c.Open();
                using (IDbCommand cmd = c.CreateCommand())
                {
                    using (DbDataAdapter adapter = _DbFactory.CreateDataAdapter())
                    {
                        cmd.CommandTimeout = 60 * 5;
                        cmd.CommandText = strSQL;
                        adapter.SelectCommand = cmd as DbCommand;
                        dt = new DataTable();
                        adapter.Fill(dt);
                        dt.Columns.Add(new DataColumn("StationID", typeof(string)) { DefaultValue = StationID });
                        dt.TableName = TaskID.ToString();
                        object val = dt.Compute(string.Format("max({0})", PrimaryKey), "");
                        _MaxId = (val == DBNull.Value ? 0 : (int)val);
                    }
                }
            }
            return dt;
        }

        /// <summary>
        /// 取系统时间
        /// </summary>
        /// <returns></returns>
        public DateTime GetDBTime()
        {
            DbProviderFactory _DbFactory = DbProviderFactories.GetFactory("Borland.Data.AdoDbxClient");
            using (IDbConnection c = _DbFactory.CreateConnection())
            {
                c.ConnectionString = ip;
                c.Open();
                using (IDbCommand cmd = c.CreateCommand())
                {
                    cmd.CommandText = "select cast( 'Now' as timestamp) from rdb$database";
                    DateTime dbtime = (DateTime)cmd.ExecuteScalar();
                    return dbtime;
                }
            }
        }
        /// <summary>
        /// 取MAX ID
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="pk">主健</param>
        /// <returns>ID</returns>
        /// 如果表名为DB_MAXID则取抽取记录的最大ID
        /// 其它则取实际的表的最大ID
        public int GetMaxID(string table, string pk)
        {
            //取Datatable里的PK主健的MAXID
            if (table == EtlGlobal.DT_MAXID)
            {
                return _MaxId;
            }
            else
            {
                //取整个表的MAXID
                DbProviderFactory _DbFactory = DbProviderFactories.GetFactory("Borland.Data.AdoDbxClient");
                using (IDbConnection c = _DbFactory.CreateConnection())
                {
                    c.ConnectionString = ip;
                    c.Open();
                    using (IDbCommand cmd = c.CreateCommand())
                    {
                        cmd.CommandText = string.Format("select max({0}) from {1}", pk, table);
                        object obj = cmd.ExecuteScalar();
                        return ((obj == DBNull.Value) ? 0 : Convert.ToInt32(obj));
                    }
                }
            }
        }

        #endregion
    }
}
